package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import controller.PlanningController;

import model.Registration;
import model.Student;
import model.container.StudentContainer;

public class ScheduleFrame extends JFrame {
	/* Required for serializable interface */
	private static final long serialVersionUID = 1L;
	/* The controller for this view */
	private PlanningController controller = PlanningController.getInstance();
	/* List of students */
	private JComboBox<Student> cboStudent;
	/* Button to request schedule for given student */
	private JButton btnGetSchedule;
	/* Button to request schedule for all students */
	private JButton btnGetScheduleAll;
	/* Table to display the schedule */
	private JTable tblSchedule;
	
	/** Constructs a new schedule frame */
	public ScheduleFrame() {
		this.setSize(800, 600);
		this.setLayout(new BorderLayout());
		this.init();
		this.loadStudents();
		this.getSchedule(null);
	}
	
	/* Builds  the frame */
	private void init() {
		// Get content pane
		Container pane = this.getContentPane();
		
		// pnStudent
		JPanel pnStudent = new JPanel(new FlowLayout());
		
		// cboStudent
		this.cboStudent = new JComboBox<Student>();
		pnStudent.add(this.cboStudent);
		
		// btnGetSchedule
		this.btnGetSchedule = new JButton("Get");
		this.btnGetSchedule.addActionListener(new BtnGetScheduleClickedListener());
		pnStudent.add(this.btnGetSchedule);
		
		// btnGetScheduleAll
		this.btnGetScheduleAll = new JButton("Get all");
		this.btnGetScheduleAll.addActionListener(new BtnGetScheduleAllClickedListener());
		pnStudent.add(this.btnGetScheduleAll);
		
		// tblSchedule
		this.tblSchedule = new JTable();
		JScrollPane scrollPane = new JScrollPane(tblSchedule);
		
		// Add components to frame
		pane.add(pnStudent, BorderLayout.NORTH);
		pane.add(scrollPane, BorderLayout.SOUTH);
	}
	
	/* Gets the schedule for a student or for all students */
	private void getSchedule(Student student) {
		Collection<Registration> schedule;
		
		// Get schedule
		if (student == null) {
			schedule = this.controller.getSchedule();
		} else {
			schedule = this.controller.getSchedule(student); 
		}
				
		// Set model for table
		ScheduleModel scheduleModel = new ScheduleModel(schedule);
		this.tblSchedule.setModel(scheduleModel);
	}
	
	/* Loads all student into the combo box */
	private void loadStudents() {
		// Clear the combobox
		this.cboStudent.removeAllItems();
		// Get a reference to the student container
		StudentContainer studentContainer = StudentContainer.getInstance();
		// Request a list of students
		ArrayList<Student> students = (ArrayList<Student>)studentContainer.getStudents();
		// Add to combobox
		for (Student student : students) {
			this.cboStudent.addItem(student);
		}
	}
	
	/** Get schedule button click handler */
	public void BtnGetScheduleClickedHandler(ActionEvent e){
		Student student = (Student)this.cboStudent.getSelectedItem();
		this.getSchedule(student);
	}
	
	/** Get schedule all button click handler */
	public void BtnGetScheduleAllClickedHandler(ActionEvent e){
		this.getSchedule(null);
	}
	
	/** Get button click listener */
	private class BtnGetScheduleClickedListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			BtnGetScheduleClickedHandler(e);
		}
	}
	
	/** Get button click listener */
	private class BtnGetScheduleAllClickedListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			BtnGetScheduleAllClickedHandler(e);
		}
	}
}